﻿using System;
using System.Data;
using System.Windows.Forms;


namespace CarBlackBox
{
    [Serializable]
    public class VehicleInfoData
    {

        public const string BRAND_Field = @"Brand";

        public const string BUYDATE_Field = @"BuyDate";

        public const string CARTYPEID_Field = @"CarTypeID";

        public const string CHECKMONTH_Field = @"CheckMonth";

        public const string FACTOR_Field = @"Factor";

        public const string LASTUPDATETIME_Field = @"LastUpdateTime";

        public const string LICENSEPLATE_Field = @"LicensePlate";

        public const string PRIMARYKEY_Field = @"LicensePlate";

        public const string LIMITSPEED_Field = @"LimitSpeed";

        public const string MEMO_Field = @"Memo";

        public const string MODEL_Field = @"Model";

        public const string OILPER_Field = @"OilPer";

        public const string OPERATOR_Field = @"Operator";

        public const string RECORDTIME_Field = @"RecordTime";

        public const string RESTTIME_Field = @"RestTime";

        public const string RIN_Field = @"RIN";

        public const string SENSOR_Field = @"Sensor";

        public const string SETUP_Field = @"Setup";

        public const string SIGN_Field = @"Sign";

        public const string SUPERTIME_Field = @"SuperTime";

        public const string TIREDTIME_Field = @"TiredTime";

        public const string VIN_Field = @"VIN";

        private String _Brand;     //厂牌

        private String _BuyDate;     //购买日期

        private Byte _CarTypeID;     //车辆分类

        private String _CheckMonth;     //年检月份

        private Int32 _Factor;     //车辆特征系数

        private DateTime _LastUpdateTime;     //最后更新时间

        private String _LicensePlate;     //车牌号码

        private Byte _LimitSpeed;     //超速阈值

        private String _Memo;     //备注

        private String _Model;     //型号

        private Single _OilPer;

        private String _Operator;     //操作员

        private Byte _RecordTime;     //行车记录间隔时间

        private Byte _RestTime;     //疲劳休息时间下限

        private String _RIN;     //记录仪序列号

        private Byte _Sensor;     //传感器

        private Boolean _Setup;     //是否已设置记录仪

        private Byte _Sign;     //信号线是否高有效

        private Int16 _SuperTime;     //超速记录时间下限

        private Byte _TiredTime;     //疲劳驾驶报警时间

        private String _VIN;     //车辆VIN

        public String Brand
        {
            get { return _Brand; }
            set { _Brand = value; }
        }
        public String BuyDate
        {
            get { return _BuyDate; }
            set { _BuyDate = value; }
        }
        public Byte CarTypeID
        {
            get { return _CarTypeID; }
            set { _CarTypeID = value; }
        }
        public String CheckMonth
        {
            get { return _CheckMonth; }
            set { _CheckMonth = value; }
        }
        public Int32 Factor
        {
            get { return _Factor; }
            set { _Factor = value; }
        }
        public DateTime LastUpdateTime
        {
            get { return _LastUpdateTime; }
            set { _LastUpdateTime = value; }
        }
        public String LicensePlate
        {
            get { return _LicensePlate; }
            set { _LicensePlate = value; }
        }
        public Byte LimitSpeed
        {
            get { return _LimitSpeed; }
            set { _LimitSpeed = value; }
        }
        public String Memo
        {
            get { return _Memo; }
            set { _Memo = value; }
        }
        public String Model
        {
            get { return _Model; }
            set { _Model = value; }
        }
        public Single OilPer
        {
            get { return _OilPer; }
            set { _OilPer = value; }
        }
        public String Operator
        {
            get { return _Operator; }
            set { _Operator = value; }
        }
        public Byte RecordTime
        {
            get { return _RecordTime; }
            set { _RecordTime = value; }
        }
        public Byte RestTime
        {
            get { return _RestTime; }
            set { _RestTime = value; }
        }
        public String RIN
        {
            get { return _RIN; }
            set { _RIN = value; }
        }
        public Byte Sensor
        {
            get { return _Sensor; }
            set { _Sensor = value; }
        }
        public Boolean Setup
        {
            get { return _Setup; }
            set { _Setup = value; }
        }
        public Byte Sign
        {
            get { return _Sign; }
            set { _Sign = value; }
        }
        public Int16 SuperTime
        {
            get { return _SuperTime; }
            set { _SuperTime = value; }
        }
        public Byte TiredTime
        {
            get { return _TiredTime; }
            set { _TiredTime = value; }
        }
        public String VIN
        {
            get { return _VIN; }
            set { _VIN = value; }
        }
        public VehicleInfoData()
        {
            _Brand = "";
            _BuyDate = "无";
            _CarTypeID = 0;
            _CheckMonth = "1月";
            _Factor = 624;
            _LastUpdateTime = DateTime.Now;
            _LicensePlate = "";
            _LimitSpeed = 80;
            _Memo = "";
            _Model = "";
            _OilPer = 10;
            _Operator = "当前操作员";
            _RecordTime = 20;
            _RestTime = 20;
            _RIN = "";
            _Sensor = 54;
            _Setup = false;
            _Sign = 255;
            _SuperTime = 0;
            _TiredTime = 240;
            _VIN = "";
        }
    }

    public class VehicleInfoDataFactory
    {
        public static VehicleInfoData Construct(IDataReader reader)
        {
            VehicleInfoData data = new VehicleInfoData();

            data.Brand = Convert.ToString(reader["Brand"]);

            data.BuyDate = Convert.ToString(reader["BuyDate"]);

            data.CarTypeID = Convert.ToByte(reader["CarTypeID"]);

            data.CheckMonth = Convert.ToString(reader["CheckMonth"]);

            data.Factor = Convert.ToInt32(reader["Factor"]);

            data.LastUpdateTime = Convert.ToDateTime(reader["LastUpdateTime"]);

            data.LicensePlate = Convert.ToString(reader["LicensePlate"]);

            data.LimitSpeed = Convert.ToByte(reader["LimitSpeed"]);

            data.Memo = Convert.ToString(reader["Memo"]);

            data.Model = Convert.ToString(reader["Model"]);

            data.OilPer = Convert.ToSingle(reader["OilPer"]);

            data.Operator = Convert.ToString(reader["Operator"]);

            data.RecordTime = Convert.ToByte(reader["RecordTime"]);

            data.RestTime = Convert.ToByte(reader["RestTime"]);

            data.RIN = Convert.ToString(reader["RIN"]);

            data.Sensor = Convert.ToByte(reader["Sensor"]);

            data.Setup = Convert.ToBoolean(reader["Setup"]);

            data.Sign = Convert.ToByte(reader["Sign"]);

            data.SuperTime = Convert.ToInt16(reader["SuperTime"]);

            data.TiredTime = Convert.ToByte(reader["TiredTime"]);

            data.VIN = Convert.ToString(reader["VIN"]);

            return data;
        }
        public static String GetPrimaryKey(DataTable dt)
        {
            return GetPrimaryKey(dt, 0);
        }
        public static String GetPrimaryKey(DataTable dt, int rowIndex)
        {
            String PrimaryKey = null;

            if (dt.Rows.Count < rowIndex + 1)

                return null;

            DataRow dr = dt.Rows[rowIndex];

            if (dr == null)

                return null;

            try { PrimaryKey = Convert.ToString(dr[VehicleInfoData.PRIMARYKEY_Field]); }
            catch { }

            return PrimaryKey;
        }
        public static VehicleInfoData Construct(DataTable dt)
        {
            return Construct(dt, 0);
        }
        public static VehicleInfoData Construct(DataTable dt, int rowIndex)
        {
            VehicleInfoData data = new VehicleInfoData();

            if (dt.Rows.Count < rowIndex + 1)

                return null;

            DataRow dr = dt.Rows[rowIndex];

            if (dr == null)

                return null;

            try { data.Brand = Convert.ToString(dr["Brand"]); }
            catch { }

            try { data.BuyDate = Convert.ToString(dr["BuyDate"]); }
            catch { }

            try { data.CarTypeID = Convert.ToByte(dr["CarTypeID"]); }
            catch { }

            try { data.CheckMonth = Convert.ToString(dr["CheckMonth"]); }
            catch { }

            try { data.Factor = Convert.ToInt32(dr["Factor"]); }
            catch { }

            try { data.LastUpdateTime = Convert.ToDateTime(dr["LastUpdateTime"]); }
            catch { }

            try { data.LicensePlate = Convert.ToString(dr["LicensePlate"]); }
            catch { }

            try { data.LimitSpeed = Convert.ToByte(dr["LimitSpeed"]); }
            catch { }

            try { data.Memo = Convert.ToString(dr["Memo"]); }
            catch { }

            try { data.Model = Convert.ToString(dr["Model"]); }
            catch { }

            try { data.OilPer = Convert.ToSingle(dr["OilPer"]); }
            catch { }

            try { data.Operator = Convert.ToString(dr["Operator"]); }
            catch { }

            try { data.RecordTime = Convert.ToByte(dr["RecordTime"]); }
            catch { }

            try { data.RestTime = Convert.ToByte(dr["RestTime"]); }
            catch { }

            try { data.RIN = Convert.ToString(dr["RIN"]); }
            catch { }

            try { data.Sensor = Convert.ToByte(dr["Sensor"]); }
            catch { }

            try { data.Setup = Convert.ToBoolean(dr["Setup"]); }
            catch { }

            try { data.Sign = Convert.ToByte(dr["Sign"]); }
            catch { }

            try { data.SuperTime = Convert.ToInt16(dr["SuperTime"]); }
            catch { }

            try { data.TiredTime = Convert.ToByte(dr["TiredTime"]); }
            catch { }

            try { data.VIN = Convert.ToString(dr["VIN"]); }
            catch { }

            return data;
        }
        public static VehicleInfoData Construct(DataGridView dgv, int rowIndex)
        {
            VehicleInfoData data = new VehicleInfoData();

            if (dgv.Rows.Count < rowIndex + 1)

                return null;

            DataGridViewCellCollection dr = dgv.Rows[rowIndex].Cells;

            if (dr == null)

                return null;

            for (int i = 0; i < dgv.Columns.Count; i++)
            {
                switch (dgv.Columns[i].DataPropertyName)
                {
                    case "Brand":
                        try { data.Brand = Convert.ToString(dr[i].Value); }
                        catch { }
                        break;
                    case "BuyDate":
                        try { data.BuyDate = Convert.ToString(dr[i].Value); }
                        catch { }
                        break;
                    case "CarTypeID":
                        try { data.CarTypeID = Convert.ToByte(dr[i].Value); }
                        catch { }
                        break;
                    case "CheckMonth":
                        try { data.CheckMonth = Convert.ToString(dr[i].Value); }
                        catch { }
                        break;
                    case "Factor":
                        try { data.Factor = Convert.ToInt32(dr[i].Value); }
                        catch { }
                        break;
                    case "LastUpdateTime":
                        try { data.LastUpdateTime = Convert.ToDateTime(dr[i].Value); }
                        catch { }
                        break;
                    case "LicensePlate":
                        try { data.LicensePlate = Convert.ToString(dr[i].Value); }
                        catch { }
                        break;
                    case "LimitSpeed":
                        try { data.LimitSpeed = Convert.ToByte(dr[i].Value); }
                        catch { }
                        break;
                    case "Memo":
                        try { data.Memo = Convert.ToString(dr[i].Value); }
                        catch { }
                        break;
                    case "Model":
                        try { data.Model = Convert.ToString(dr[i].Value); }
                        catch { }
                        break;
                    case "OilPer":
                        try { data.OilPer = Convert.ToSingle(dr[i].Value); }
                        catch { }
                        break;
                    case "Operator":
                        try { data.Operator = Convert.ToString(dr[i].Value); }
                        catch { }
                        break;
                    case "RecordTime":
                        try { data.RecordTime = Convert.ToByte(dr[i].Value); }
                        catch { }
                        break;
                    case "RestTime":
                        try { data.RestTime = Convert.ToByte(dr[i].Value); }
                        catch { }
                        break;
                    case "RIN":
                        try { data.RIN = Convert.ToString(dr[i].Value); }
                        catch { }
                        break;
                    case "Sensor":
                        try { data.Sensor = Convert.ToByte(dr[i].Value); }
                        catch { }
                        break;
                    case "Setup":
                        try { data.Setup = Convert.ToBoolean(dr[i].Value); }
                        catch { }
                        break;
                    case "Sign":
                        try { data.Sign = Convert.ToByte(dr[i].Value); }
                        catch { }
                        break;
                    case "SuperTime":
                        try { data.SuperTime = Convert.ToInt16(dr[i].Value); }
                        catch { }
                        break;
                    case "TiredTime":
                        try { data.TiredTime = Convert.ToByte(dr[i].Value); }
                        catch { }
                        break;
                    case "VIN":
                        try { data.VIN = Convert.ToString(dr[i].Value); }
                        catch { }
                        break;
                }
            }
            return data;
        }
        public static String GetPrimaryKey(DataGridView dgv, int rowIndex)
        {
            String PrimaryKey = null;

            if (dgv.Rows.Count < rowIndex + 1)

                return null;

            DataGridViewCellCollection dr = dgv.Rows[rowIndex].Cells;

            if (dr == null)

                return null;

            for (int i = 0; i < dgv.Columns.Count; i++)
            {
                if (dgv.Columns[i].DataPropertyName == VehicleInfoData.PRIMARYKEY_Field)
                {
                    try { PrimaryKey = Convert.ToString(dr[i].Value); }
                    catch { }
                    return PrimaryKey;
                }
            }
            return PrimaryKey;
        }
    }
}